package com.cx.controller;

import com.cx.common.ApiReturn;
import com.cx.pojo.Car;
import com.cx.pojo.CarInfoModel;
import com.cx.service.CarService;
import com.cx.util.QiniuCloudUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("car")
public class CarController {

    @Autowired
    CarService carService;

    //添加汽车
    @RequestMapping("insertCar")
    @ResponseBody//将数据转换成json后返回给前端
    public ApiReturn insertCar(Car car){
        int rows = carService.insert(car);
        if (rows<1){
            return ApiReturn.error("添加汽车失败");
        }
        return ApiReturn.success("添加汽车成功");
    }

    //删除汽车
    @RequestMapping("deleteCar")
    @ResponseBody
    public ApiReturn deleteCar(Integer carId) {
        int rows = carService.deleteByPrimaryKey(carId);
        if (rows < 1) {
            return ApiReturn.error("删除汽车失败");
        }
        return ApiReturn.success("删除汽车成功");
    }

    //修改汽车
    @RequestMapping("updateCar")
    @ResponseBody
    public ApiReturn updateCar(Car car) {
        int rows = carService.updateByPrimaryKey(car);
        if (rows < 1) {
            return ApiReturn.error("修改汽车失败");
        }
        return ApiReturn.success("修改汽车成功");
    }

    //根据carId查询汽车
    @RequestMapping("selectCarById")
    @ResponseBody
    public ApiReturn selectCarById(Integer carId) {
        Car carInfo = carService.selectByPrimaryKey(carId);
        if (carInfo != null) {
            return ApiReturn.success("汽车数据详情",carInfo);
        }
        return ApiReturn.error("未获取该汽车数据详情");
    }

    //查询所有汽车
    @RequestMapping("selectAllCar")
    @ResponseBody
    public ApiReturn selectAllCar(CarInfoModel carInfoModel) {
        List<CarInfoModel> carInfoModels = carService.selectAllCar(carInfoModel);
        if (carInfoModels !=null){
            return ApiReturn.success("返回汽车数据",carInfoModels);
        }
        return ApiReturn.error("无数据");
    }

    //查询所有汽车
    @RequestMapping("CarManage")
    @ResponseBody
    public ApiReturn CarManage(CarInfoModel carInfoModel) {
        PageInfo<CarInfoModel> page = carService.CarManage(carInfoModel);
        if (page.getList() !=null){
            return ApiReturn.success("返回汽车数据",page);
        }
        return ApiReturn.error("无数据");
    }

    //七牛云图片上传
    @RequestMapping("uploadPic")
    @ResponseBody
    public ApiReturn uploadPic(@RequestParam("file") MultipartFile file) throws IOException {
        if (file!=null){
            File file1 = new File("E:\\test");
            file.transferTo(file1);
            String imgName = String.valueOf(System.currentTimeMillis());
            String url = QiniuCloudUtil.upload(file1,imgName);
            file1.delete();
            return ApiReturn.success("上传成功",imgName);
        }
        return ApiReturn.error("上传失败");
    }
}
